//
//  LeftCollectionViewCell.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/27.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit

protocol LeftCollectionViewCellDelegate {
    func letSelfGotoProductVCWithStr(id:String,name:String)
}

class LeftCollectionViewCell: UICollectionViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    
    var delegate: LeftCollectionViewCellDelegate?
    var myCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewLayout())
    
    let SECONDCELL = "secondCell"
    var myDataArr = NSArray()
    
    
    var dataArray : NSArray?{
        didSet{
            myDataArr = dataArray!
            self.myCollectionView.reloadData()
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.95-64))
        
        self.creatCollectionView()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension LeftCollectionViewCell{
    private func creatCollectionView(){
        let layout  = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake((SCREEN_WIDTH*0.75-20)/3, (SCREEN_WIDTH*0.75-20)/3*1.4)
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5)
        layout.scrollDirection = .Vertical
        
        myCollectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.95-64)
        myCollectionView.collectionViewLayout = layout
        myCollectionView.backgroundColor = UIColor.whiteColor()
        myCollectionView.delegate = self
        myCollectionView.dataSource = self
        self.addSubview(myCollectionView)
        let nib = UINib(nibName: "SecondLeftCollectionViewCell", bundle: nil)
        
        myCollectionView.registerNib(nib, forCellWithReuseIdentifier: SECONDCELL)
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.myDataArr.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SECONDCELL, forIndexPath: indexPath)
        
        
        let model = self.myDataArr[indexPath.item]as?LeftModel
        (cell as? SecondLeftCollectionViewCell)?.model = model
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let model = self.myDataArr[indexPath.item] as? LeftModel
        
        
        self.delegate?.letSelfGotoProductVCWithStr((model?.id)!, name: (model?.categories_name)!)
        
    }
    
}
















